package org.example.audio;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.Frame;
/**
 * @author zehua
 * @date 2023/11/7 10:14
 * @Description TODO
 * @since V1.1.0
 */

/**
 * 移除音频
 */
public class RemoveAudio {
    public static void main(String[] args) {
        String inputPath = "C:\\Users\\Administrator\\Downloads\\十大名劍，邪劍，妖刀 _ 老高與小茉 Mr & Mrs Gao.mp4"; // 替换为你的输入视频文件路径
        String outputPath = "C:\\Users\\Administrator\\Downloads\\noAudio.mp4"; // 替换为你想要保存的无音频视频文件路径

        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(inputPath);
        FFmpegFrameRecorder recorder = null;

        try {
            grabber.start();
            recorder = new FFmpegFrameRecorder(outputPath, grabber.getImageWidth(), grabber.getImageHeight(), 0); // 0表示不包括音频
            recorder.setFrameRate(grabber.getFrameRate());
            recorder.setVideoCodec(grabber.getVideoCodec());
            recorder.setFormat(grabber.getFormat());
            recorder.setVideoBitrate(grabber.getVideoBitrate());
            recorder.start();

            Frame frame;
            while ((frame = grabber.grabImage()) != null) {
                recorder.record(frame);
            }

            recorder.stop();
            grabber.stop();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (recorder != null) {
                try {
                    recorder.release();
                } catch (FFmpegFrameRecorder.Exception e) {
                    e.printStackTrace();
                }
            }
            if (grabber != null) {
                try {
                    grabber.release();
                } catch (FFmpegFrameGrabber.Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

